package com.nextive.weightlogger.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.nextive.weightlogger.R;

/**
 * A dialog to input a weight value, containing two number pickers, one for the
 * integer part and the other for the decimal part.
 * 
 * This dialog takes one decimal.
 * 
 * @author Federico Baseggio <fedebaseggio@gmail.com>
 */
public class WeightPickerDialog extends AlertDialog {
	/**
	 * Interface used to allow the creator of the WeightPickerDialog dialog to
	 * run some code when the user enters and confirm a weight value.
	 * 
	 * @author Federico Baseggio <fedebaseggio@gmail.com>
	 */
	public interface OnWeightSetListener {
		/**
		 * Callback method invoked when the user enters and confirms a weight
		 * value.
		 * 
		 * @param weight The weight value that the user entered.
		 */
		public void onWeightSet(float weight);
	}

	/**
	 * The number picker widget for the integer part of the weight value.
	 */
	NumberPickerWrapper intNumberPicker;

	/**
	 * The number picker widget for the decimal part of the weight value.
	 */
	NumberPickerWrapper decimalNumberPicker;

	/**
	 * Constructor.
	 * 
	 * @param context The context that launches this dialog, usually an Activity.
	 * @param onWeightSetListener How the parent is notified that the weight is set.
	 * @param currentValue The weight value to be set in the dialog upon launch.
	 * @param minValue The minimum value to be accepted.
	 * @param maxValue The maximum value to be accepted.
	 */
	public WeightPickerDialog(Context context,
			final OnWeightSetListener onWeightSetListener, Float currentValue,
			int minValue, int maxValue) {
		super(context);

		LayoutInflater inflater = this.getLayoutInflater();
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.weight_picker_dialog, null);

		intNumberPicker = new NumberPickerWrapper(context,
				(LinearLayout) layout.findViewById(R.id.integerNumberPicker));		
		decimalNumberPicker = new NumberPickerWrapper(context,
				(LinearLayout) layout.findViewById(R.id.decimalNumberPicker));		

		intNumberPicker.setRange(minValue, maxValue);
		decimalNumberPicker.setRange(0, 9);

		if (currentValue != null) {
			setValue(currentValue);
		}

		setView(layout);
		setTitle(R.string.weight_picker_dialog_title);

		setButton(BUTTON_POSITIVE, context.getText(R.string.dialog_set),
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						onWeightSetListener.onWeightSet(getValue());
					}
				});

		setButton(BUTTON_NEGATIVE, context.getText(R.string.dialog_cancel),
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Just let the dialog be dismissed
					}
				});
	}
	
	/**
	 * Sets the weight value.
	 * 
	 * @param The weight value.
	 */
	public void setValue(float value)
	{
		int integerValue = (int) Math.floor(value);
		
		int decimalValue = (int) Math.round((value - integerValue) * 10);
		if (decimalValue > 9)
		{
			decimalValue = 9;
		}
		intNumberPicker.setCurrent(integerValue);
		decimalNumberPicker.setCurrent(decimalValue);
	}

	/**
	 * Retrieves the weight value selected by the user.
	 * 
	 * @return The weight value.
	 */
	public float getValue() {
		return (float) Math.floor(intNumberPicker.getCurrent() * 10
				+ decimalNumberPicker.getCurrent()) / 10;
	}
}
